from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
import re,redis,random,string
# Create your views here.
class Reg(APIView):
    def post(self, request):
        mobile = request.data.get('mobile')
        if not all([mobile]):
            return Response({
                'code': 400,
                'msg': '所需字段不完整'
            })
        if not re.findall(r'^1[3-9]\d{9}$', mobile):
            return Response({'code': 400, 'msg': '手机号格式不正确'})
        user_data = Sj.objects.filter(mobile=mobile)
        if user_data:
            return Response({
                'code': 400,
                'msg': '该手机号已存在'
            })
        Sj.objects.create(
            mobile=mobile
        )
        return Response({
            'code': 200,
            'msg': '注册成功'
        })

class Log(APIView):
    def get(self, reqeust):
        mobile = reqeust.data.get('mobile')
        if not all([mobile]):
            return Response({
                'code': 400,
                'msg': '所需字段不完整'
            })
        if not re.findall(r'^1[3-9]\d{9}$', mobile):
            return Response({'code': 400, 'msg': '手机号格式不正确'})
        user_data = Sj.objects.filter(mobile=mobile)
        print(user_data)
        if not user_data:
            return Response({
                'code': 400,
                'msg': '手机号不存在'
            })
        return Response({
            'code': 200,
            'msg': '登录成功'
        })

class Tj(APIView):
    def get(self,request):
        data=Xq.objects.all()
        list=[]
        for i in data:
            list.append({
                'id':i.id,
                'name':i.name,
                'sfz':i.sfz,
                'xb': i.xb,
                'rq': i.rq,
                'sj_id': i.sj_id,
                'jzdz': i.jzdz,
                'na': i.na,
            })
        return Response(list)

    def post(self,request):
        name=request.data.get('name')
        sfz = request.data.get('sfz')
        xb = request.data.get('xb')
        rq = request.data.get('rq')
        sj_id = request.data.get('sj_id')
        jzdz = request.data.get('jzdz')
        na = request.data.get('na')
        Xq.objects.create(
            name=name,
            sfz=sfz,
            xb=xb,
            rq=rq,
            sj_id=sj_id,
            jzdz=jzdz,
            na=na
        )
        return Response({'code':200,'msg':'添加成功'})
class Xg(APIView):
    def put(self,request,id):
        name = request.data.get('name')
        sfz = request.data.get('sfz')
        xb = request.data.get('xb')
        rq = request.data.get('rq')
        sj_id = request.data.get('sj_id')
        jzdz = request.data.get('jzdz')
        na = request.data.get('na')
        Xq.objects.filter(id=id).update(
            name=name,
            sfz=sfz,
            xb=xb,
            rq=rq,
            sj_id=sj_id,
            jzdz=jzdz,
            na=na
        )
        return Response({'code': 200, 'msg': '修改成功'})
    def delete(self,request,id):
        Xq.objects.get(id=id).delete()
        return Response({'code':200,'msg':'删除成功'})
